<template>
  <div class="common-layout">
    <el-container>
      <el-aside width="200px">
        <el-menu active-text-color="#ffd04b" background-color="#545c64" class="el-menu-vertical-demo" default-active="1"
          text-color="#fff" router>
          <div class="title">智企后台</div>
          <el-menu-item index="home">
            <el-icon><icon-menu /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="overview" v-if="userStore.identity == '超级管理员'">
            <el-icon>
              <Document />
            </el-icon>
            <span>系统概览</span>
          </el-menu-item>
          <el-sub-menu index="3" v-if="userStore.identity == '超级管理员' || userStore.identity == '用户管理员'">
            <template #title>
              <el-icon>
                <User />
              </el-icon>
              <span>用户管理</span>
            </template>
            <el-menu-item-group title="管理员管理" v-if="userStore.identity == '超级管理员'">
              <el-menu-item index="product_manager">产品管理员</el-menu-item>
              <el-menu-item index="message_manager">消息管理员</el-menu-item>
              <el-menu-item index="user_manager">用户管理员</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group title="员工管理">
              <el-menu-item index="user_list">用户列表</el-menu-item>
            </el-menu-item-group>
          </el-sub-menu>
          <el-sub-menu index="4"
            v-if="userStore.identity == '超级管理员' || userStore.identity == '产品管理员' || userStore.identity == '用户'">
            <template #title>
              <el-icon>
                <TakeawayBox />
              </el-icon>
              <span>产品管理</span>
            </template>
            <el-menu-item-group title="入库管理">
              <el-menu-item index="product_manage">产品列表</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group title="出库管理">
              <el-menu-item index="out_product_manage">出库列表</el-menu-item>
            </el-menu-item-group>
          </el-sub-menu>
          <el-sub-menu index="5" v-if="userStore.identity == '超级管理员' || userStore.identity == '消息管理员'">
            <template #title>
              <el-icon>
                <ChatSquare />
              </el-icon>
              <span>消息管理</span>
            </template>
            <el-menu-item-group title="消息管理">
              <el-menu-item index="message_list">消息列表</el-menu-item>
            </el-menu-item-group>
            <el-menu-item-group title="出库管理">
              <el-menu-item index="recycle">回收站</el-menu-item>
            </el-menu-item-group>
          </el-sub-menu>
          <el-menu-item index="file" v-if="userStore.identity == '超级管理员'">
            <el-icon><icon-menu /></el-icon>
            <span>合同管理</span>
          </el-menu-item>
          <el-menu-item index="operation_log" v-if="userStore.identity == '超级管理员'">
            <el-icon><icon-menu /></el-icon>
            <span>操作日志</span>
          </el-menu-item>
          <el-menu-item index="login_log" v-if="userStore.identity == '超级管理员'">
            <el-icon><icon-menu /></el-icon>
            <span>登录日志</span>
          </el-menu-item>
          <el-menu-item index="set">
            <el-icon><icon-menu /></el-icon>
            <span>系统设置</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <div class="header-left-content">尊敬的 {{ userStore.name || `用户${userStore.account}` }} 欢迎你登录智企后台</div>
          <div class="header-right-content">
            <el-badge :is-dot="isRead" class="item" @click="showMsgDialog">
              <el-icon :size="24" class="message">
                <Message />
              </el-icon>
            </el-badge>
            <el-avatar :size="24" :src="circleUrl" />
            <el-dropdown>
              <span class="el-dropdown-link">
                设置
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item>设置账号</el-dropdown-item>
                  <el-dropdown-item>更改头像</el-dropdown-item>
                  <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        <el-main>
          <RouterView></RouterView>
        </el-main>
      </el-container>
    </el-container>
  </div>
  <departmentMsg ref="departmentMsgRef" @updateisRead="changeisRead"></departmentMsg>
</template>

<script lang="ts" setup>
import Vue from 'vue'
import { computed, ref, watch } from 'vue'
import { Menu as IconMenu } from '@element-plus/icons-vue'
import { reactive, toRefs } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useUserInfo } from '../../store/userinfo'
import departmentMsg from '../../components/departmentMsg.vue'
import { getReadListAPI, getDepartmentMsgAPI } from '../../api/department_msg'
import { msgInfo } from '../../store/msglist';
import { Store } from "pinia"


const msgStore = msgInfo() as Store<"msginfo", { msgList: string }, { msgInfo }>

const departmentMsgRef = ref()

const userStore = useUserInfo()

const router = useRouter()

const state = reactive({
  circleUrl: computed(() => userStore.imageUrl),
  sizeList: ['small', '', 'large'] as const,
})
const { circleUrl } = toRefs(state)

// 退出登录
const logout = async () => {
  try {
    await ElMessageBox.confirm('你确定退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
    confirmlogout()
  } catch (error) {
    console.log(error)
  }
}
const confirmlogout = () => {

  ElMessage({
    type: 'success',
    message: '已成功退出登录',
  })
  localStorage.clear()
  setTimeout(() => {
    router.push('/login')
  }, 1000)
}


let isRead = ref(false)

const changeisRead = (value: boolean) => {
  isRead.value = value
}
// 显示未读消息弹窗
const showMsgDialog = () => {
  departmentMsgRef.value.open()
}


let department = JSON.parse(localStorage.getItem("userinfo") as string)?.department
let msgList = JSON.parse(localStorage.getItem("msginfo") as string)?.msgList


// 新用户登录没有部门 防止报错
if (department != '' && department != null) {
  if (msgList != null) {
    if (msgList.length > 0) {
      isRead.value = true
    } else {
      isRead.value = false
    }
  } else {
    // console.log('该用户没有部门 or 已读全部')
  }
} 
</script>

<style lang="scss" scoped>
.el-aside {
  height: 100vh;
  width: 210px;
  background-color: #545c64;

  .el-menu {
    width: 210px;
    height: 100vh;
  }

  .title {
    color: #fff;
    text-align: center;
    font-size: 20px;
    padding: 20px;
  }
}

.el-header {
  display: flex;
  height: 55px;
  background: #545c64;
  color: #fff;
  align-items: center;
  justify-content: space-between;

  .header-left-content {
    font-size: 14px;
  }

  .header-right-content {
    width: 160px;
    display: flex;
    justify-content: space-around;
    align-items: center;
  }

  .el-dropdown {
    color: #fff;
  }
}

.item {
  cursor: pointer;
}
</style>